package com.withoutwords.ui.custom;

import java.util.ArrayList;
import java.util.Random;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Guess;
import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.SocialUser;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class GuessesBoardLayout extends RelativeLayout implements
		IGuessClickListener {

	// final static int maxGuesses= 16;
	IGuessClickListener guessListener;
	boolean bApproveEnabled;
	ArrayList<GuessLayout> guessesLayouts;
	Random rand = new Random(hashCode());

	public void setApproveEnabled(boolean bApproveEnabled) {
		this.bApproveEnabled = bApproveEnabled;
	}

	public void setGuessListener(IGuessClickListener guessListener) {
		this.guessListener = guessListener;
	}

	public GuessesBoardLayout(Context context) {
		super(context);
		init();

	}

	public GuessesBoardLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}

	public void setBackgroundBitmap() {
		ViewGroup rly = (ViewGroup) findViewById(R.id.lyGuessBoard);
		if (rly != null) {
			rly.setBackgroundDrawable(BitmapCache.getCacheInstance()
					.getDrawable(this.getContext(),R.drawable.blackboard));
		}
	}

	public void unSetBackgroundBitmap() {
		ViewGroup rly = (ViewGroup) findViewById(R.id.lyGuessBoard);
		if (rly != null) {
			rly.setBackgroundDrawable(null);
		}
	}

	void init() {
		bApproveEnabled = false;
		Game currentGame = GameController.getInstance().getCurrentGame();
		if (currentGame != null) {

			SocialUser owner = currentGame.getGameOwner();

			String ownerId = owner.getSocialID();
			MyInfo me = Settings.getInstance().getMyInfo();

			if (me != null && owner != null) {
				SocialUser socialMe = me.getSocialUser();
				if (socialMe != null) {
					String mySocialId = socialMe.getSocialID();
					bApproveEnabled = (ownerId != null && mySocialId != null && mySocialId
							.equalsIgnoreCase(ownerId));
				}
			}
		}
		guessesLayouts = new ArrayList<GuessLayout>();
		LayoutInflater inf = LayoutInflater.from(getContext());
		inf.inflate(R.layout.guessboard, this);
		ViewGroup rly = (ViewGroup) findViewById(R.id.lyGuessBoard);
		if (rly != null) {

			for (int i = 0; i < rly.getChildCount(); i++) {
				View v = rly.getChildAt(i);
				if (GuessLayout.class.isInstance(v)) {
					v.setVisibility(INVISIBLE);
					guessesLayouts.add((GuessLayout) v);
				}
			}
		}

	}

	GuessLayout getRandomGuessLayout() {

		int nRandomIndex = rand.nextInt(guessesLayouts.size());
		GuessLayout gly = guessesLayouts.get(nRandomIndex);
		while (gly.getVisibility() == VISIBLE) {
			nRandomIndex = rand.nextInt(guessesLayouts.size());
			gly = guessesLayouts.get(nRandomIndex);

		}
		gly.setVisibility(VISIBLE);
		return gly;
	}

	public void addGuess(Guess guess) {
		if (guess != null) {
			GuessLayout gl = getRandomGuessLayout();
			gl.setGuess(guess, this, bApproveEnabled);
			gl.invalidate();
		}

	}

	@Override
	public void onActionApprovedClicked(Guess guess) {
		// guessesLayouts.remove(guess);
		if (guessListener != null) {
			guessListener.onActionApprovedClicked(guess);
		}
		invalidate();
	}

	@Override
	public void onActionDeclinedClicked(Guess guess) {
		// guessesLayouts.remove(guess);
		if (guessListener != null) {
			guessListener.onActionDeclinedClicked(guess);
		}
		invalidate();

	}

}
